
using System;
using System.Collections.Generic;
using System.Linq;
using MonoTouch.Foundation;
using MonoTouch.UIKit;

using com.ReinforceLab.AudioModem;
using com.ReinforceLab.AudioModem.FSK;

namespace ArduinoiPhoneShield
{
	public partial class TextingTestPanel : UIViewController
	{
        FSKModem _modem;        

		#region Constructors
		// The IntPtr and initWithCoder constructors are required for controllers that need 
		// to be able to be created from a xib rather than from managed code

		public TextingTestPanel (IntPtr handle) : base(handle)
		{
			Initialize ();
		}

		[Export("initWithCoder:")]
		public TextingTestPanel (NSCoder coder) : base(coder)
		{
			Initialize ();
		}

		public TextingTestPanel () : base("TextingTestPanel", null)
		{
			Initialize ();
		}

		void Initialize ()
		{
		}
		
		#endregion

        public override void ViewDidAppear(bool animated)
        {
            base.ViewDidAppear(animated);
            
            // loading modem
            _modem = new FSKModem(BaudRate.B1200, null); // FIXME
            var phy = new AudioQueueAudioPHY(_modem.SamplingRate);
            _modem.Start(phy);

            // set event handlers
            _sendButton.TouchUpInside += new EventHandler(_sendButton_TouchUpInside);
            _clearButton.TouchUpInside += new EventHandler(_clearButton_TouchUpInside);
        }
        void _clearButton_TouchUpInside(object sender, EventArgs e)
        {
            _textBox.Text = string.Empty;            
        }
        void _sendButton_TouchUpInside(object sender, EventArgs e)
        {                
            var data = System.Text.Encoding.ASCII.GetBytes(_textBox.Text);
            //_modem.Write(data); FIXME
        }

        public override void ViewWillDisappear(bool animated)
        {
            base.ViewWillDisappear(animated);
                        
            _modem.Dispose();
            _modem = null;            
        }
	}
}

